<template>
  <div class="user-layout">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-content">
        <div class="logo">
          <img src="../assets/logo.png" alt="Logo">
          <span>宠物管理系统</span>
        </div>
        <nav class="nav-menu">
          <router-link to="/user/home" class="nav-item">首页</router-link>
          <router-link to="/user/pet-list" class="nav-item">宠物浏览</router-link>
          <router-link to="/user/shop" class="nav-item">商城</router-link>
          <router-link to="/user/service-reserve" class="nav-item">服务预约</router-link>
          <router-link to="/user/foster-reserve" class="nav-item">宠物寄养</router-link>
          <router-link to="/user/pet-health" class="nav-item">健康档案</router-link>
        </nav>
        <div class="header-right">
          <el-badge :value="cartCount" class="cart-badge" v-if="isLogin">
            <el-button icon="el-icon-shopping-cart-2" circle></el-button>
          </el-badge>
          <div class="user-info" v-if="isLogin">
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                <el-avatar :size="32" :src="userAvatar">{{ username }}</el-avatar>
                <span style="margin-left: 10px">{{ username }}</span>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="center">个人中心</el-dropdown-item>
                <el-dropdown-item command="orders">我的订单</el-dropdown-item>
                <el-dropdown-item command="favorites">我的收藏</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="login-buttons" v-else>
            <el-button type="primary" size="small" @click="goToLogin">登录</el-button>
            <el-button size="small" @click="goToRegister">注册</el-button>
          </div>
        </div>
      </div>
    </header>

    <!-- 主内容区 -->
    <main class="main-content">
      <router-view></router-view>
    </main>

    <!-- 底部 -->
    <footer class="footer">
      <div class="footer-content">
        <div class="footer-section">
          <h4>关于我们</h4>
          <p>专业的宠物服务平台</p>
          <p>为您的爱宠提供全方位服务</p>
        </div>
        <div class="footer-section">
          <h4>联系我们</h4>
          <p>电话：400-123-4567</p>
          <p>邮箱：service@petmanage.com</p>
        </div>
        <div class="footer-section">
          <h4>关注我们</h4>
          <p>微信公众号：宠物管家</p>
          <p>微博：@宠物管理系统</p>
        </div>
      </div>
      <div class="footer-bottom">
        <p>© 2024 宠物管理系统. All Rights Reserved.</p>
      </div>
    </footer>
  </div>
</template>

<script>
import { logout } from '@/api/auth'

export default {
  name: 'UserLayout',
  data () {
    return {
      cartCount: 0,
      username: '',
      userAvatar: '',
      isLogin: false
    }
  },
  created () {
    this.checkLoginStatus()
  },
  methods: {
    checkLoginStatus () {
      const token = localStorage.getItem('token')
      if (token) {
        this.isLogin = true
        // 从localStorage获取用户信息
        const userInfo = localStorage.getItem('userInfo')
        if (userInfo) {
          try {
            const user = JSON.parse(userInfo)
            this.username = user.realName || user.username
          } catch (e) {
            this.username = localStorage.getItem('username') || '用户'
          }
        } else {
          this.username = localStorage.getItem('username') || '用户'
        }
      }
    },
    handleCommand (command) {
      switch (command) {
        case 'center':
          this.$router.push('/user/user-center')
          break
        case 'orders':
          this.$router.push('/user/user-center')
          break
        case 'favorites':
          this.$message.info('我的收藏')
          break
        case 'logout':
          this.handleLogout()
          break
      }
    },
    handleLogout () {
      this.$confirm('确认退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用退出登录API
        logout().then(() => {
          // 清除本地存储
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          this.isLogin = false
          this.$message.success('已退出登录')
          this.$router.push('/login')
        }).catch(() => {
          // 即使API调用失败，也清除本地token并跳转
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          this.isLogin = false
          this.$router.push('/login')
        })
      })
    },
    goToLogin () {
      this.$router.push('/login')
    },
    goToRegister () {
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped>
.user-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: bold;
  color: #409EFF;
}

.logo img {
  height: 40px;
}

.nav-menu {
  display: flex;
  gap: 30px;
  flex: 1;
  justify-content: center;
}

.nav-item {
  color: #333;
  text-decoration: none;
  font-size: 16px;
  padding: 5px 10px;
  transition: all 0.3s;
}

.nav-item:hover,
.nav-item.router-link-active {
  color: #409EFF;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.cart-badge {
  line-height: 1;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.main-content {
  flex: 1;
}

.footer {
  background: #333;
  color: white;
  padding: 40px 20px 20px;
  margin-top: 50px;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-around;
  margin-bottom: 30px;
}

.footer-section h4 {
  margin-bottom: 15px;
  font-size: 18px;
}

.footer-section p {
  margin: 8px 0;
  color: #ccc;
  font-size: 14px;
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #555;
  color: #999;
}
</style>
